package com.dnjn.happiness.biz.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dnjn.happiness.biz.domain.AdviceTags;
import com.dnjn.happiness.biz.domain.AdviceUserTag;
import com.dnjn.happiness.biz.mapper.AdviceTagsMapper;
import com.dnjn.happiness.biz.service.IAdviceTagsService;
import com.dnjn.happiness.biz.service.IAdviceUserTagService;
import com.dnjn.happiness.common.core.constant.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 模块功能Service业务层处理
 *
 * @author happiness
 * @date 2023-10-14
 */
@Service
public class AdviceTagsServiceImpl extends ServiceImpl<AdviceTagsMapper, AdviceTags> implements IAdviceTagsService {

    @Autowired
    AdviceTagsMapper adviceTagsMapper;
    @Autowired
    IAdviceUserTagService adviceUserTagService;

    @Override
    public int saveTags(AdviceTags adviceTags) {

        //先保存Tags
        int count = adviceTagsMapper.insert(adviceTags);
        List<Long> userIds = adviceTags.getUserIds();
        if (!userIds.isEmpty() && count > 0) {
            Long adviceTagsId = adviceTags.getAdviceTagsId();
            ArrayList<AdviceUserTag> list = new ArrayList<>();
            userIds.stream().forEach(item -> {
                AdviceUserTag userTag = new AdviceUserTag();
                userTag.setAdviceTagsId(adviceTagsId);
                userTag.setUserId(item);
                list.add(userTag);

            });

            adviceUserTagService.saveBatch(list);
        }
        return count;


    }

    @Override
    public Page<AdviceTags> pageCommentVo(Integer pageNum, Integer pageSize, String label) {
//        Map<String, Object> example = new HashMap<>();
//        example.put("feedbackType", label);
        Page<AdviceTags> pageCommentVo = adviceTagsMapper.pageCommentVoByMap(new Page(pageNum, pageSize), label);
        return pageCommentVo;
    }

    @Override
    public int updateAdviceTagById(AdviceTags adviceTags) {
        //先保存Tags
        int count = adviceTagsMapper.updateById(adviceTags);

        //删掉之前的
        Long adviceTagsId = adviceTags.getAdviceTagsId();
        adviceUserTagService.remove(Wrappers.lambdaQuery(AdviceUserTag.class).eq(AdviceUserTag::getAdviceTagsId, adviceTagsId));
        //再加上
        List<Long> userIds = adviceTags.getUserIds();
        if (!userIds.isEmpty() && count > 0) {
            ArrayList<AdviceUserTag> list = new ArrayList<>();
            userIds.stream().forEach(item -> {
                AdviceUserTag userTag = new AdviceUserTag();
                userTag.setAdviceTagsId(adviceTagsId);
                userTag.setUserId(item);
                list.add(userTag);

            });

            adviceUserTagService.saveBatch(list);
        }
        return count;

    }

    @Override
    public AdviceTags getAdviceTagsById(Long adviceTagsId) {
        return adviceTagsMapper.getAdviceTagsById(adviceTagsId);

    }

    @Override
    public List<AdviceTags> selectByDeptId(Long deptId) {
        return baseMapper.selectByDeptId(deptId);
    }

    @Override
    public List<AdviceTags> queryList() {

        return lambdaQuery().eq(AdviceTags::getDeleted, Constants.UNDELETED).list();
    }

    @Override
    public Page<AdviceTags> selectAdviceTags(AdviceTags domain, Page page) {
        return adviceTagsMapper.selectAdviceTags(domain, page);
    }


}

